<?php
/**
 * pagetemplate.php
 * Create on 2015-03-26
 * Create by yuebin
 */
namespace haibao\cms\data\site;

class PageTemplate extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\site\PageTemplate');
    }
    
    /**
     * 添加站点频道
     * @param : Object \haibao\cms\model\data\site\PageTemplate $pageTemplateModel
     */
    public function addPageTemplate($pageTemplateModel){
    	
    	$this->beginTranscation();
    	try{
    		$this->add($pageTemplateModel);
    	} catch(\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    /**
     * 根据站点ID更新站点频道
     * @param : Object \haibao\cms\model\data\site\SiteChannel $pageTemplateModel
     */
    public function updatePageTemplate($pageTemplateModel){
    	$arrSet =
    	array(
    			\haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_TEMPLATE_NAME => $pageTemplateModel->TemplateName,
    	        \haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_TEMPLATE_PATH => $pageTemplateModel->TemplatePath,
    			\haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_CONTENT => $pageTemplateModel->Content,
    			\haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_UPDATE_TIME => $pageTemplateModel->UpdateTime,
    			\haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_PAGEABLE => $pageTemplateModel->Pageable
    			
    	);
    	$this->beginTranscation();
    	try{
    
    		$this->updateById($pageTemplateModel->Id, $arrSet);
    	
    		 
    	} catch(\Exception $e){
    		 
    		$this->rollback();
    		throw $e;
    		 
    	}
    	$this->endTranscation();
    }
    
    /**
     * 删除页面模版
     * @param : Int $id
     * @param : Int $status
     */
    public function deletePageTmpById($id, $status = \haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE){
    	 
    	$this->updateById($id,
    			array(
    				\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_STATUS => $status
    			)
    	);
    }

    /**
     * 根据模版名字获取模版信息
     * @param : String $channelName
     * @param : Int $siteId
     */
    public function getPageTmpByTmpName($templateName,$templateId = null){
    	$model = null;
    	$sql = "select * from `PageTemplate` where TemplateName=%s";
    	$valArr = array($templateName);
    	if($templateId){
    		$sql .= " and Id <>%s";
    		array_push($valArr,$templateId);
    	}
    	$result = $this->query($sql, $valArr);
    	while ($result->fetch_assoc()){
    		$model = new \haibao\cms\model\data\site\PageTemplate();
    		foreach($model->getKeyMap() as $keyName=>$keyType){
    			$model->$keyName = $result->getData($keyType->getFieldName());
    		}
    	}
    	return $model;
    }
    
    /**
     * 判断模版使用被频道使用
     * @param : Int $id 模版ID
     */
    public function whetherUseForTemplate($id){
    	
    	$sql = "select Id from `SiteChannelTemplate` where TemplateId=%s limit 1";
    	$result = $this->query($sql,array($id))->fetch_assoc();
    	if($result){
    		return true;
    	}
    	return false;
    }
}
